<template>
  <el-drawer
    v-model="dialogVisible"
    title="审核"
    width="600px"
    @close="reset"
  >
    <el-form 
      ref="articleFormRef"
      :model="tableItem" 
      :rules="rules"
      label-width="180"
      label-position="top"
    >
      <el-form-item label="审核状态" prop="status">
        <el-radio-group v-model="tableItem.status">
          <el-radio :label="2">通过</el-radio>
          <el-radio :label="3">拒绝</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="tableItem.remark" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="updateData"
        >确 定</el-button>
      </span>
    </template>
  </el-drawer>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { examineEnterprise } from '@/api/settle'
import { validate } from '@/utils/validate'

const tableItem = reactive({
  id: '',
  status: 2,
  remark: ''
})

const rules = reactive({
  status: [{ required: true, message: '请选择审核状态', trigger: 'change' }]
})

const dialogVisible = ref(false)
const dialogStatus = ref('edit')

const reset = () => {
  if (!articleFormRef.value) return
  articleFormRef.value.resetFields()
  tableItem.status = 2
  tableItem.remark = ''
}

const articleFormRef = ref(null)

const emit = defineEmits(['listChange'])

const updateData = async () => {
  const valid = await validate(articleFormRef)
  if (valid) {
    examineEnterprise(tableItem)
    .then(() => {
      emit('listChange')
      dialogVisible.value = false
      ElMessage.success('审核成功！')
    })
    .catch((err) => {
      ElMessage.error(err.msg)
    })
  }
}

defineExpose({ dialogVisible, tableItem, dialogStatus, reset })
</script>